/**
 * 2018年11月27日下午9:21:38
 * @author 谢进财
 */
package com.baihang.project.ds.service;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baihang.framework.dao.Record;
import com.baihang.framework.kit.JsonKit;
import com.baihang.framework.kit.ModelKit;
import com.baihang.framework.kit.StrKit;
import com.baihang.framework.service.BaseService;
import com.baihang.project.ds.dao.CustomerThreeDao;
import com.baihang.project.ds.model.RenChouModel;
import com.baihang.project.ds.model.SalesManModel;
import com.baihang.project.ds.model.TUserModel;

import net.sf.json.JSONObject;

@Service
public class CustomerThreeService extends BaseService {
	private static Logger logger = LoggerFactory.getLogger(CustomerThreeService.class);
	@Autowired
	private CustomerThreeDao dao;
	
	public String getGrid(int page,int pagesize,String sortname,String sortorder,String where) {
		String sqlConut =String.format("select count(*) from t_user where D_Source=0 ");
		String resultSql =String.format("select * from t_user where D_Source=0 order by %s %s limit %d,%d ", sortname,sortorder,(page-1)*pagesize,pagesize);
		if(StrKit.isNotEmpty(where)) {
			sqlConut =String.format("select count(*) from t_user where D_Source=0 and (D_Name like '%s' or D_Tel like '%s')",where,where);
			resultSql =String.format("select * from t_user where D_Source=0 and (D_Name like '%s' or D_Tel like '%s') order by %s %s limit %d,%d ",where,where,sortname,sortorder,(page-1)*pagesize,pagesize);
		}
		int count = dao.queryForInt(sqlConut);
	
		List<Record> list = dao.queryForRecordList(resultSql);		
		String jsonRecord = JsonKit.listToJson(list, 1);
		StringBuilder sb = new StringBuilder("{");
		sb.append("\"Rows\":"+jsonRecord+",");
		sb.append("\"Total\":"+count);
		sb.append("}");
		return sb.toString();
	}
	
	public List<SalesManModel> getSaleMan(){
		List<Record> list = dao.queryForRecordList("select * from saller");
		List<SalesManModel> list2 = new ArrayList<SalesManModel>();
		if(list.size()>0) {
			for(Record rec:list) {
				SalesManModel model = new SalesManModel();
				model.setId(rec.getInt("D_ID"));
				model.setName(rec.getStr("D_Name"));
				model.setTel(rec.getStr("D_Tel"));
				list2.add(model);
			}
		}
		return list2;
	}
	public List<RenChouModel> getRenChou(){
		List<Record> list = dao.queryForRecordList("select * from t_renchou");
		List<RenChouModel> list2 = new ArrayList<RenChouModel>();
		if(list.size()>0) {
			for(Record rec:list) {
				RenChouModel model = new RenChouModel();
				model.setId(rec.getInt("id"));
				model.setName(rec.getStr("name"));
				model.setMoney(rec.getStr("money"));
				list2.add(model);
			}
		}
		return list2;
	}
	
	public boolean deleteCustomerThree(String tel) {
		String sql =String.format("delete from t_user  where D_Tel='%s'", tel);
		
		int result = dao.delete(sql);			
		if(result>0) {
			return true;
		}
		return false;
	}
	
	public boolean validateTelCustomerThree(String tel) {
		String sql =String.format("select count(*) from t_user  where D_Tel='%s'", tel);
		
		int result = dao.queryForInt(sql);		
		if(result>0) {
			return false;
		}
		return true;
	}
	public String validateIdCardCustomerThree(String card) {
		String sql =String.format("select * from t_user  where D_Card='%s' limit 1", card);
		
		Record rec = dao.queryForRecord(sql);
		if(rec ==null) {
			return String.format("{\"result\":\"%s\",\"msg\":\"%s\"}", "true","ok");
		}else {
			return String.format("{\"result\":\"%s\",\"msg\":\"%s\",\"json\":%s}", "false","该客户已经是主购人!",JsonKit.toJson(rec));
		}
	}
	
	public boolean saveCustomerThree(String json) {
		JSONObject jsonObject = JSONObject.fromObject(json);
		TUserModel tuser = new TUserModel();
		tuser.setD_Card(jsonObject.getString("ccard"));
		//tuser.setD_FromType(jsonObject.getString("D_FromType"));
		tuser.setD_Name(jsonObject.getString("cname"));
		tuser.setD_Sex(jsonObject.getString("csex"));
		tuser.setD_Source("0");
		tuser.setD_Tel(jsonObject.getString("tel"));
		tuser.setD_Saller(jsonObject.getString("saller"));
		try {
			dao.save(ModelKit.modelToSaveSql(tuser));
		}catch (Exception e) {
			logger.info(e.getMessage());
			return false;
		}
		return true;
	}
}
